package model;

import init.DBManagement;

import java.util.Map;
import java.util.Vector;

import business.misc.Util;

import model.beans.ExamenBean;

public class Exam {
	public static ExamenBean instance( int int_examId ){
		ExamenBean exam_retVal = null;
		String str_instance = 
			" select LUGAR, "+
			"        to_char( FECHA, '" + Util.str_format + "' ) AS FECHA, "+
			"        hora, "+
			"        materia_tk "+
			" from	 plaexar "+
			" where  EXA_TECH_KEY = "+int_examId+" ";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_instance );
	    if( database.getNumRows() == 1 ){
	    	exam_retVal = new ExamenBean();
	    	exam_retVal.setLugar(database.getItem(0, 0));
	    	String str_fecha = database.getItem(0, 1);
	    	exam_retVal.setFechaCal(Util.stringToCalendar(str_fecha));
	    	exam_retVal.setHora(database.getItem(0, 2));
	    	exam_retVal.setSubject_id(database.getItem(0, 3));
	    }
	    database = null;
		return exam_retVal;
	}
	
	public static Vector list(String str_academy, String str_subject){
		Vector vec_retVal = new Vector();
		String str_list = 
			" select EXA_TECH_KEY, "+
			"		 LUGAR, "+
			"		 to_char( FECHA, '" + Util.str_format + "' ) AS FECHA, "+
			"		 HORA "+
			" from	 plaexar "+
			" where  MATERIA_TK = '"+str_subject+"'";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_list );
	    for( int int_i = 0; int_i < database.getNumRows(); int_i++){
	    	ExamenBean exam = new ExamenBean();
	    	exam.setId(Integer.parseInt(database.getItem(int_i, 0)));
	    	exam.setLugar(database.getItem(int_i, 1));
	    	exam.setFechaCal(Util.stringToCalendar(database.getItem(int_i, 2)));
	    	exam.setHora(database.getItem(int_i, 3));
	    	exam.setSubject_id(str_subject);
	    	exam.setAcademy_id(str_academy);
	    	vec_retVal.add(exam);
	    }
		return vec_retVal;
	}
	
	public static int add(Map map_data, String str_subjectId){
		int int_add;
		String str_lugar = (String)map_data.get("lugar");
		String str_fecha = (String)map_data.get("fecha");
        String str_hora  = (String)map_data.get("hora");
		String str_add =
            " INSERT INTO plaexar ( "+
            "        MATERIA_TK, "+
            "        LUGAR, "+
            "        FECHA, "+
            "        HORA  "+
            " ) VALUES ( "+
            "        '"+str_subjectId+"', "+
            "        '"+str_lugar+"'," +
            "        to_date( '"+str_fecha+"', '"+Util.str_format+"')," +
            "        '"+str_hora+"'" +
            " ) ";
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_add );
        int_add = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_add;
	}
	
	public static int edit( Map map_data ){
		int int_edit;
		int int_id = ((Integer)map_data.get("exam_id")).intValue();
		String str_lugar = (String)map_data.get("lugar");
		String str_fecha = (String)map_data.get("fecha");
        String str_hora  = (String)map_data.get("hora");
		String str_edit =
            " update plaexar "+
            " set    LUGAR = '"+str_lugar+"', "+
            "        FECHA = to_date( '"+str_fecha+"', '"+Util.str_format+"'), "+
            "        HORA = '"+str_hora+"' "+
            " where  EXA_TECH_KEY = "+int_id;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_edit );
        int_edit = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_edit;
	}
	
	public static int delete( int int_examId ){
		int int_delete;
		
		String str_delete =
            " delete from plaexar "+
            " where  EXA_TECH_KEY = "+int_examId;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_delete );
        int_delete = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_delete;
	}
}
